package com.qianfeng.zookeeper.zookeeper.watcher;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by jackiechan on 2021/7/9 11:53
 *
 * @author jackiechan
 * 请记住 2021-06-24 这个让我变成 SB 的日子
 */

public class ZkWatcher  implements Watcher {

    private ZooKeeper zooKeeper;
    private static final String ZKADDR = "10.9.12.200:8601,10.9.12.200:8602,10.9.12.200:8603";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);//用作阻塞的, 1 的意思是只需有一个地方执行了放行就放行所有阻塞的地方


    public ZkWatcher() throws Exception {
        System.err.println("连接 zk 服务器");
        zooKeeper = new ZooKeeper(ZKADDR, 5000, this);
        zooKeeper.exists("/03cacheservice",true);

    }

    @Override
    public void process(WatchedEvent event) {
        Event.KeeperState state = event.getState();
        Event.EventType type = event.getType();
        String path = event.getPath();
        if (state == Event.KeeperState.SyncConnected && type == Event.EventType.None) {
            System.err.println("连接zk 服务器成功");
        } else if (state == Event.KeeperState.SyncConnected && type == Event.EventType.NodeCreated) {

            try {
                System.err.println("收到节点:" + path + "创建成功的事件");
                zooKeeper.getChildren("/03cacheservice",true);
                zooKeeper.getData("/03cacheservice",true,null);
            }catch (Exception e){
               e.printStackTrace();
            }
        } else if (state == Event.KeeperState.SyncConnected && type == Event.EventType.NodeDataChanged) {
            try {
                byte[] data = zooKeeper.getData(path, true, null);
                System.err.println("收到节点:" + path + "数据变化的事件"+new String(data));
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else if (state == Event.KeeperState.SyncConnected && type == Event.EventType.NodeChildrenChanged) {

            try {
                List<String> stringList = zooKeeper.getChildren(path, true);
                System.err.println("收到节点:" + path + "子节点变化的事件"+stringList);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
